package com.ggh.jinjilive.ui.home.video.videoplay.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemVodeoVp2Binding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.home.video.VideoList;
import com.ggh.jinjilive.ui.home.video.videoplay.VideoPlayActivity;
import com.ggh.jinjilive.views.video.TikTokView;
import com.ggh.jinjilive.views.video.cache.PreloadManager;
import com.google.gson.Gson;
import com.makeramen.roundedimageview.RoundedImageView;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import xyz.doikki.videocontroller.StandardVideoController;
import xyz.doikki.videoplayer.exo.ExoMediaPlayerFactory;
import xyz.doikki.videoplayer.ijk.IjkPlayerFactory;
import xyz.doikki.videoplayer.player.AndroidMediaPlayerFactory;

public class VideoPlayAdapter extends RecyclerView.Adapter<VideoPlayAdapter.ItemViewHolder>{
    private List<VideoList> videoListList;

    public VideoPlayAdapter(List<VideoList> videoListList){
        this.videoListList = videoListList;

    }


    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }



    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vodeo_vp2, parent, false);
        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ItemViewHolder holder, int position) {
        VideoList videoList = videoListList.get(position);
        if(null != videoList){
            holder.mPosition = position;
            PreloadManager.getInstance(holder.itemView.getContext()).addPreloadTask(videoList.getMovieurls(), position);
        }
    }


    @Override
    public void onViewDetachedFromWindow(@NonNull ItemViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        VideoList videoList = videoListList.get(holder.mPosition);
        if(null != videoList){
            PreloadManager.getInstance(holder.itemView.getContext()).removePreloadTask(videoList.getMovieurls());
        }

    }

    @Override
    public int getItemCount() {
        return videoListList != null ? videoListList.size() : 0;

    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        public int mPosition;

        public TikTokView mTikTokView;
        public FrameLayout mPlayerContainer;
        //标题与内容
        public TextView tv_nickname;
        public TextView tv_title;
        //头像
        public RoundedImageView riv;
        public ImageView img_attention;
        //点赞
        public LinearLayout ll_point_praise;
        public ImageView img_point_praise;
        public TextView likeText;
        //消息
        public LinearLayout ll_message;
        public TextView commentText;
        //分享
        public LinearLayout ll_share;
        public TextView shareNum;

        ItemViewHolder(View itemView) {
            super(itemView);
            mTikTokView = itemView.findViewById(R.id.tiktok_View);
            tv_nickname = mTikTokView.findViewById(R.id.tv_nickname);
            tv_title = mTikTokView.findViewById(R.id.tv_title);
            //头像
            riv = mTikTokView.findViewById(R.id.riv);
            img_attention = mTikTokView.findViewById(R.id.img_attention);
            //点赞
            ll_point_praise = mTikTokView.findViewById(R.id.ll_point_praise);
            img_point_praise = mTikTokView.findViewById(R.id.img_point_praise);
            likeText = mTikTokView.findViewById(R.id.likeText);
            //消息
            ll_message = mTikTokView.findViewById(R.id.ll_message);
            commentText = mTikTokView.findViewById(R.id.commentText);
            //分享
            ll_share = mTikTokView.findViewById(R.id.ll_share);
            shareNum = mTikTokView.findViewById(R.id.shareNum);


            mPlayerContainer = itemView.findViewById(R.id.container);
            itemView.setTag(this);
        }
    }


}
